//
//  SISSRepaymentRedemptionViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/25.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSRepaymentRedemptionViewController.h"
#import "SISSRepaymentRedemptionViewModel.h"
#import "SISSRepaymentRedemptionShowViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSRepaymentRedemptionViewController ()

/* 确定按钮 */
@property (weak, nonatomic) UIButton *confirmButton;

@property (strong, nonatomic) SISSRepaymentRedemptionViewModel *repaymentRedemptionViewModel;


@end

@implementation SISSRepaymentRedemptionViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"还款赎楼";
}

#pragma mark - 代理
#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.repaymentRedemptionViewModel getSectionCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.repaymentRedemptionViewModel getRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //    NSArray *arr = self.dataArr[indexPath.section];
    return [self.repaymentRedemptionViewModel getCell:tableView indexPath:indexPath];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return [self.repaymentRedemptionViewModel getHeightForHeaderInSection:section];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [self.repaymentRedemptionViewModel getViewHeaderInSection:section];
}

// 取消tableView组头悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

/**
 必选提示
 
 @return 返回YES时需要填写或选择
 */
- (BOOL)requiredTip {

    SISSRepaymentRedemptionModel *repaymentRedemptionModel = [self.repaymentRedemptionViewModel dataArrToRepaymentRedemptionModel];

    if (!repaymentRedemptionModel.bank || repaymentRedemptionModel.bank.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入买方贷款银行" andTime:2 andColor:YES];
        return YES;
    }
    if (!repaymentRedemptionModel.amount || repaymentRedemptionModel.amount.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入放款金额" andTime:2 andColor:YES];
        return YES;
    }
    if (!repaymentRedemptionModel.loanDate || repaymentRedemptionModel.loanDate == 0) {
        [SISSTools showSVErrorTitle:@"请选择放款日期" andTime:2 andColor:YES];
        return YES;
    }
    if (!repaymentRedemptionModel.repaymentAmount || repaymentRedemptionModel.repaymentAmount == 0) {
        [SISSTools showSVErrorTitle:@"请输入还款本金" andTime:2 andColor:YES];
        return YES;
    }
    if (!repaymentRedemptionModel.interest || repaymentRedemptionModel.interest == 0) {
        [SISSTools showSVErrorTitle:@"请输入罚息金额" andTime:2 andColor:YES];
        return YES;
    }
    if (!repaymentRedemptionModel.repaymentDate || repaymentRedemptionModel.repaymentDate.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择还款日期" andTime:2 andColor:YES];
        return YES;
    }
    if (self.repaymentRedemptionModel) {
        repaymentRedemptionModel.ID = self.repaymentRedemptionModel.ID;
    }
    self.repaymentRedemptionModel = repaymentRedemptionModel;
    return NO;
}


#pragma mark - 按钮点击事件
#pragma mark -- 确定按钮点击事件
- (void)confirmButtonClickAction {
    
    if ([self requiredTip]) return;
    NSString *loanDate = [self.repaymentRedemptionModel.loanDate stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
     NSString *repaymentDate = [self.repaymentRedemptionModel.repaymentDate stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    if (self.repaymentRedemptionModel.ID.length > 0) {
        NSDictionary *parameters = @{
                                     @"id": self.repaymentRedemptionModel.ID,
                                     @"spId": [SISSUserAccount shared].spId,
                                     @"ahbpId": self.ahbpId,
                                     @"amount": self.repaymentRedemptionModel.amount,
                                     @"bank": self.repaymentRedemptionModel.bank,
                                     @"interest": self.repaymentRedemptionModel.interest,
                                     @"loanDate": loanDate,
                                     @"repaymentAmount": self.repaymentRedemptionModel.repaymentAmount,
                                     @"repaymentDate": repaymentDate,
                                     @"otherdesc": self.repaymentRedemptionModel.otherdesc.length ? self.repaymentRedemptionModel.otherdesc : @""
                                     };
        
        [kNetworkManager updateRepaymentInfoWithParamters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSRepaymentRedemptionShowViewController *vc = [[SISSRepaymentRedemptionShowViewController alloc] init];
                vc.ahbpId = self.ahbpId;
                vc.dataSource = self.repaymentRedemptionViewModel.dataArr;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *parameters = @{
                                     @"spId": [SISSUserAccount shared].spId,
                                     @"ahbpId": self.ahbpId,
                                     @"amount": self.repaymentRedemptionModel.amount,
                                     @"bank": self.repaymentRedemptionModel.bank,
                                     @"interest": self.repaymentRedemptionModel.interest,
                                     @"loanDate": loanDate,
                                     @"repaymentAmount": self.repaymentRedemptionModel.repaymentAmount,
                                     @"repaymentDate": repaymentDate,
                                     @"otherdesc": self.repaymentRedemptionModel.otherdesc.length ? self.repaymentRedemptionModel.otherdesc : @""
                                     };
        
        [kNetworkManager insertRepaymentInfoWithParamters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSRepaymentRedemptionShowViewController *vc = [[SISSRepaymentRedemptionShowViewController alloc] init];
                vc.ahbpId = self.ahbpId;
                vc.dataSource = self.repaymentRedemptionViewModel.dataArr;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.repaymentRedemptionViewModel = [[SISSRepaymentRedemptionViewModel alloc] init];
    if (self.dataSource.count) {
        self.repaymentRedemptionViewModel.dataArr = self.dataSource.copy;
    }
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] init];
    tableView.estimatedRowHeight = 44;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.repaymentRedemptionViewModel registContractClass:tableView];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.repaymentRedemptionViewModel.tableView = tableView;
    
    UIView *footerView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 88)];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(footerView);
//        make.width.mas_equalTo(SCREEN_WIDTH);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        make.height.mas_equalTo(44);
    }];
    
    self.tableView.tableFooterView = footerView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}


@end
